using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum REGION{
	USA = 0,
	EU = 1,
	ASIA = 2,
	JAPAN = 3
}

public class ControlRoom : Photon.MonoBehaviour {
	
	public GameObject objChatManager;
	
	private ChatManager scripsChatManager;

	private Hashtable customRoomPropperties;
	private string [] roomPropsInLobby = {"channel"};
	
	
	
	private Rect	button_OPEN_CHANNEL_RECT,
					button_CLOSE_CHANNEL_RECT,
					SEVERINFO_RECT,
					GRID_RECT;
	
	private bool isChannelOpening = false;
	
	private int selRegionInt = 2;
	public string[] selRegion;
	
	void Start () {
		customRoomPropperties = new Hashtable(){{"channel",1}};
		
		button_OPEN_CHANNEL_RECT = new Rect(Screen.width*0.3f,Screen.height*0.4f,Screen.width*0.4f,Screen.height*0.2f);
		button_CLOSE_CHANNEL_RECT = new Rect(Screen.width*0.8f,0f,Screen.width*0.2f,Screen.height*0.2f);
		SEVERINFO_RECT	=	new Rect(0,0,Screen.width*0.4f,Screen.height*0.3f);
		
		scripsChatManager = objChatManager.GetComponent<ChatManager>();
		
		selRegion = new string[]{"Sever - USA","Sever - EU","Sever - ASIA","Sever - JAPAN"};
		GRID_RECT = new Rect(Screen.width*0.3f,Screen.height*0.65f,Screen.width*0.4f,Screen.height*0.25f);
	}

	void OnGUI(){
		
		if (!isChannelOpening){
			GUILayout.BeginArea(button_OPEN_CHANNEL_RECT);
			if (GUILayout.Button("Open Channel",GUILayout.Height(Screen.height*0.1f))){
				
				switch (selRegionInt){
					case 2:
						PhotonNetwork.PhotonServerSettings.ServerAddress = "app-asia.exitgamescloud.com";
						break;
					case 1:
						PhotonNetwork.PhotonServerSettings.ServerAddress = "app-us.exitgamescloud.com";
						break;
					case 3:
						PhotonNetwork.PhotonServerSettings.ServerAddress = "app-jp.exitgamescloud.com";
						break;
					case 0:
						PhotonNetwork.PhotonServerSettings.ServerAddress = "app-us.exitgamescloud.com";
						break;
					default :
						PhotonNetwork.PhotonServerSettings.ServerAddress = "app-asia.exitgamescloud.com";
						break;
				}
				
				PhotonNetwork.ConnectUsingSettings("1.0");
				isChannelOpening = true;
			}
			
			GUILayout.EndArea();
			
			selRegionInt = GUI.SelectionGrid(GRID_RECT,selRegionInt,selRegion,1);
			
			
		}
		else{
			GUILayout.BeginArea(button_CLOSE_CHANNEL_RECT);
			if (GUILayout.Button("Close Channel",GUILayout.Height(Screen.height*0.1f))){
				isChannelOpening = false;
				scripsChatManager.isChatActive = false;
				scripsChatManager.listChatLog.Clear();
				
				foreach(PhotonPlayer player in PhotonNetwork.playerList){
					PhotonNetwork.CloseConnection(player);	
				}
				
				photonView.RPC("synDisconectedByAdmin",PhotonTargets.All);
			}
			GUILayout.EndArea();
			
			GUILayout.BeginArea(SEVERINFO_RECT);
			GUILayout.Box(PhotonNetwork.PhotonServerSettings.ServerAddress);
			if (PhotonNetwork.room != null){
				GUILayout.Box(PhotonNetwork.room.name);
			}
			GUILayout.EndArea();
		}
		
	}
	
	public void OnCreatedRoom()
    {
        scripsChatManager.isChatActive = true;
    }

	public void OnConnectedToPhoton(){
		StartCoroutine(CreateRoom());
		
	}
	
	void OnLeftRoom(){
		Debug.Log("Why kick me !!!!");
	}
	
	[RPC]
	void synDisconectedByAdmin(){
		Debug.Log("All Player were disconected...!!!");
		StartCoroutine(DisconectToSever());
	}
	
	IEnumerator CreateRoom(){
		WaitForSeconds t = new WaitForSeconds(1);
		yield return t;
		int indexRoom = PhotonNetwork.countOfRooms+1;
		PhotonNetwork.CreateRoom("Heroes - Age of Magic [Channel " + indexRoom + "]",true,true,20,customRoomPropperties,roomPropsInLobby);
		PhotonNetwork.playerName = "[Admin - TJ]";
	}
	
	IEnumerator DisconectToSever(){
		WaitForSeconds t = new WaitForSeconds(1);
		yield return t;
		PhotonNetwork.Disconnect();
	}
}
	